﻿using B.S.Medical.Api.Write.Application.Command.Patient;
using B.S.Medical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Medical.Api.Write.Controllers.Patient
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PatientController : ControllerBase
    {
        private readonly IMediator mediator;

        public PatientController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加就诊人
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPatient(AddPatientCommand request, CancellationToken cancellationToken)
        {
            // 从Claims获取当前登录用户Id
            var userIdClaim = User.Claims.FirstOrDefault(c => c.Type == "sub" || c.Type == "UserId" || c.Type == "AccountId");
            if (userIdClaim != null && long.TryParse(userIdClaim.Value, out long accountId))
            {
                request.AccountId = accountId;
            }
            // 若未获取到，可根据实际需求抛异常或默认值
            return await mediator.Send(request);
        }
    }
}
